﻿using AutoMapper;
using MediatR;
using Process.Api.Application.Command;
using Process.BLL;
using Process.DAL;
using Process.ErrorCod;
using Process.Model;

namespace Process.Api.Application.Handler
{
	public class DetailProcessTypeHandler : IRequestHandler<DetailProcessTypeCommand, ApiResult<ProcessType>>
	{
		private readonly IProcessRepository<ProcessType> processTypeRepo;
		private readonly IMapper mapper;
		private readonly ILogger<DetailProcessTypeHandler> logger;

		public DetailProcessTypeHandler(IProcessRepository<ProcessType> processTypeRepo, IMapper mapper, ILogger<DetailProcessTypeHandler> logger)
		{
			this.processTypeRepo = processTypeRepo;
			this.mapper = mapper;
			this.logger = logger;
		}
		/// <summary>
		/// 根据Id查询工序分类
		/// </summary>
		public Task<ApiResult<ProcessType>> Handle(DetailProcessTypeCommand request, CancellationToken cancellationToken)
		{
			var result = new ApiResult<ProcessType>();

			var entity = processTypeRepo.GetAllAsync().FirstOrDefault(x => x.Id == request.Id && x.IsDelete == false);
			if (entity == null)
			{
				result.Code = ErrorStatus.Error;
				result.Message = "未找到对应工序分类";
				return Task.FromResult(result);
			}

			result.Code = ErrorStatus.Success;
			result.Data = entity;
			result.Message = "获取成功";
			return Task.FromResult(result);
		}
	}
}
